package com.ruoyi.fly.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.fly.domain.Product;
import com.ruoyi.fly.domain.ProductCategory;
import com.ruoyi.fly.domain.StoreProductServer;
import com.ruoyi.fly.domain.vo.*;
import com.ruoyi.fly.service.ProductCategoryService;
import com.ruoyi.fly.service.ProductService;
import com.ruoyi.fly.service.StoreProductService;
import com.ruoyi.fly.service.StoreServerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productCategory")
@Api(tags = "产品管理")
public class ProductController extends BaseController {


    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProductService productService;



    /**
     * 查询产品分类列表
     */
    @ApiOperation(value = "查询产品分类列表")
    @PostMapping("/categoryList")
    public TableDataInfo categoryList(@RequestBody ProductCategorySearchVo productCategorySearchVo) {
        PageDomain pageDomain = new PageDomain();
        BeanUtils.copyProperties(productCategorySearchVo,pageDomain);
        startPage(pageDomain);
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategorySearchVo);
        return getDataTable(list);
    }

    @ApiOperation(value = "查询产品分类列表")
    @PostMapping("/selectList")
    public R selectList(@RequestBody ProductCategorySearchVo productCategorySearchVo) {
        productCategorySearchVo.setStatus("1");
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategorySearchVo);
        return R.ok(list);
    }

    /**
     * 获取产品分类详细信息
     */
    @ApiOperation(value = "根据id获取产品分类详细信息")
    @GetMapping(value = "/sel/{id}")
    public R selectCategoryById(@PathVariable("id") String id) {
        if(productCategoryService.selectProductCategoryById(id)==null){
            return R.fail("产品分类不存在");
        }
        return R.ok(productCategoryService.selectProductCategoryById(id));
    }

    /**
     * 新增产品分类
     */
    @ApiOperation(value = "新增产品分类")
    @PostMapping("/insertCategory")
    public R addCategory(@RequestBody ProductCategory productCategory) {
        return productCategoryService.insertProductCategory(productCategory);
    }

    /**
     * 修改产品分类
     */
    @ApiOperation(value = "修改产品分类")
    @PostMapping("/update")
    public R editCategory(@RequestBody ProductCategory productCategory) {
        return productCategoryService.updateProductCategory(productCategory);
    }

    /**
     * 删除产品分类
     */
    @ApiOperation(value = "删除产品分类")
    @PostMapping("/delete")
    public R removeCategory(@RequestBody DeleteVo deleteVo) {
        return productCategoryService.deleteProductCategoryByIds(deleteVo.getIds());
    }


    /**
     * 查询商品列表
     */
    @ApiOperation(value = "查询商品列表")
    @PostMapping("/selProductList")
    public TableDataInfo productList(@RequestBody ProductSearchVo productSearchVo) {
        PageDomain pageDomain = new PageDomain();
        BeanUtils.copyProperties(productSearchVo,pageDomain);
        startPage(pageDomain);
        List<ProductVo> list = productService.selectProductList(productSearchVo);
        return getDataTable(list);
    }

    /**
     * 根据id获取商品详细信息
     */
    @ApiOperation(value = "根据id获取商品详细信息")
    @GetMapping(value = "/selectProductById/{id}")
    public R selectProductById(@PathVariable("id") String id) {
        return R.ok(productService.selectProductById(id));
    }

    /**
     * 新增商品
     */
    @ApiOperation(value = "新增商品")
    @PostMapping("/addProduct")
    public R addProduct(@RequestBody Product product) {
        return productService.insertProduct(product);
    }

    /**
     * 修改商品
     */
    @ApiOperation(value = "修改商品")
    @PostMapping("/editProduct")
    public R editProduct(@RequestBody Product product) {
        return productService.updateProduct(product);
    }

    /**
     * 删除商品
     */
    @ApiOperation(value = "删除商品")
    @PostMapping("/deleteProduct")
    public R removeProduct(@RequestBody DeleteVo deleteVo) {
        return productService.deleteProductByIds(deleteVo.getIds());
    }

    /**
     * 添加店铺商品/服务
     * @param productInsertVo
     * @return
     */
    @ApiOperation(value = "添加店铺商品/服务")
    @PostMapping("/insertStoreProductOrServer")
    public R insertStoreProduct(@RequestBody ProductInsertVo productInsertVo) {
        return productService.insertStoreProduct(productInsertVo);
    }

    /**
     * 查询店铺下的商品/服务列表
     */
    @ApiOperation(value = "查询店铺下的商品/服务列表")
    @PostMapping("/selStoreProductList")
    public R<StoreProductServer> selStoreProductList(@RequestBody StoreProductServerVo storeProductServerVo) {
        return productService.selectStoreProductServerList(storeProductServerVo);
    }


}
